package com.kehao.leetcode.middle.math;

import org.junit.Test;

public class MySqrtSolution {

    private boolean isAnswer(long ans,int x){
        return ans*ans<=x && (ans+1)*(ans+1)>x;
    }

    public int mySqrt(int x) {
        if(x==1) return 1;
        double ans = x/2;
        while (true){
            if(isAnswer((int)ans,x)) return (int)ans;
            ans = ans-((ans*ans-x)/(2*ans));
        }
    }

    @Test
    public void test01(){
        System.out.println(mySqrt(2147395600));
    }
}
